<?php

// File : random_text_generator.php

include_once("file_utilities.php");

const STYLESHEET = "style3.css";
const TITLE = "Random text generator";
const LANGUAGE_LIST_FILE = "languages.txt";
const LANGUAGE_NAME_KEY = "language_name";
const DICTIONARY_FILE_KEY = "dictionary_file";
const LENGTH_KEY = "length";
const PARAGRAPH_LENGTH_KEY = "paragraph_length";

class RandomTextGenerator
{
// Public methods
    public function __construct($language, $language_list_file)
    {
        $this->m_language = $language;
        
        $language_array = $this->read_languages($language_list_file);
        $language_count = count($language_array);
        
        $dictionary_file = "";
        for ($i = 0; $i < $language_count; $i++)
        {
            if ($language_array[$i][LANGUAGE_NAME_KEY] == $language)
            {
                $dictionary_file = $language_array[$i][DICTIONARY_FILE_KEY];
                break;
            }
        }
        
        $part_files = read_lines($dictionary_file, TRUE, TRUE);
        $part_count = count($part_files);
    
        $temp_array = array($part_count);
        for ($i = 0; $i < $part_count; $i++)
        {
            $temp_array[$i] = array();
        
            $part_file = $part_files[$i];
        
            $parts = read_lines($part_file, TRUE, TRUE);
            $line_count = count($parts);
            for ($j = 0; $j < $line_count; $j++)
            {
                $temp_array[$i][] = $parts[$j];
            }
        }
        $this->m_text_array = $temp_array;
    }
    
    public function get_language_name()
    {
        return $this->m_language;
    }
    
    public function get_random_text($sentence_count, $paragraph_length = 0)
    {
        $array = $this->m_text_array;
    
        $part_count = count($array);
        $return_value = "";
    
        for($i = 0; $i < $sentence_count; $i++)
        {
            for($j = 0; $j < $part_count; $j++)
            {
                if ($i == 0 && $j == 0)
                {
                    $return_value = $return_value . $array[0][0];
                }
                else
                {
                    $k = rand(0, count($array[$j]) - 1);
                    $return_value = $return_value . $array[$j][$k];
                }
                $return_value = $return_value . " ";
            }
            if ($paragraph_length > 0 && ($i + 1) % $paragraph_length == 0)
                $return_value = $return_value . "<br /><br />";
        }
    
        return $return_value;
    }
    
    public static function read_languages($filename)
    {
        $temp_array = read_lines($filename, TRUE, TRUE);
        $language_count = count($temp_array) / 2;
        $return_array = array($language_count);
    
        for ($i = 0; $i < $language_count; $i++)
        {
            $return_array[$i] = array();
            $return_array[$i][LANGUAGE_NAME_KEY] = $temp_array[$i * 2];
            $return_array[$i][DICTIONARY_FILE_KEY] = $temp_array[$i * 2 + 1];
        }
    
        return $return_array;
    }
    
    public static function get_input_form()
    {
        $return_value = "<form method=";
        $return_value = $return_value . get_double_quoted_text("get");
        
        $return_value = $return_value . " action=";
        $return_value = $return_value . get_double_quoted_text("show_text.php");
        
        $return_value = $return_value . " name=";
        $return_value = $return_value . get_double_quoted_text("input_form");
        
        $return_value = $return_value . " onsubmit=";
        $return_value = $return_value . get_double_quoted_text("return validate_input()");
        
        $return_value = $return_value . ">";
        $return_value = $return_value . "<strong>Random text generator</strong><br />";
        
        $return_value = $return_value . "Please select language:<br />";
        $language_list = RandomTextGenerator::read_languages(LANGUAGE_LIST_FILE);
        $n = count($language_list);
        for ($i = 0; $i < $n; $i++) 
        {
            $language = $language_list[$i][LANGUAGE_NAME_KEY];
            $filename = $language_list[$i][DICTIONARY_FILE_KEY];
            
            $return_value = $return_value . "<input type=". get_double_quoted_text("radio");
            $return_value = $return_value . " name=" . get_double_quoted_text(LANGUAGE_NAME_KEY);
            $return_value = $return_value . " value=" . get_double_quoted_text($language);
            $return_value = $return_value . ">" . $language . "<br />";
        }
        
        $return_value = $return_value . "Please enter length of text (# of sentences):<br />";
        
        $return_value = $return_value . "<input type=" . get_double_quoted_text("text");
        $return_value = $return_value . " name=" . get_double_quoted_text(LENGTH_KEY);
        $return_value = $return_value . " value=" . get_double_quoted_text("5") . " />";
        
        $return_value = $return_value . "<br />";
        
        $return_value = $return_value . "Please enter paragraph length (0 for no breaks):<br />";
        
        $return_value = $return_value . "<input type=" . get_double_quoted_text("text");
        $return_value = $return_value . " name=" . get_double_quoted_text(PARAGRAPH_LENGTH_KEY);
        $return_value = $return_value . " value=" . get_double_quoted_text("0") . " />";
        
        $return_value = $return_value . "<br />";
        
        $return_value = $return_value . "<input type=" . get_double_quoted_text("submit");
        $return_value = $return_value . " value=" . get_double_quoted_text("GO!") . " />";
        
        $return_value = $return_value . "</form>";
        
        return $return_value;
    }
    
// Private data
    
    private $m_language;
    private $m_text_array;
}

?>